/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.gs.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.config.FileProperties;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.gs.domain.GShelves;
import me.zhengjie.gs.service.GShelvesService;
import me.zhengjie.gs.service.dto.GShelvesQueryCriteria;
import me.zhengjie.utils.FileUtil;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.gs.service.dto.GShelvesDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author yun
* @date 2024-02-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "测试题1管理")
@RequestMapping("/api/gShelves")
public class GShelvesController {

    private final GShelvesService gShelvesService;
    
    private final FileProperties properties;
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('gShelves:list')")
    public void exportGShelves(HttpServletResponse response, GShelvesQueryCriteria criteria) throws IOException {
        gShelvesService.download(gShelvesService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询测试题1")
    @ApiOperation("查询测试题1")
    @PreAuthorize("@el.check('gShelves:list')")
    public ResponseEntity<PageResult<GShelvesDto>> queryGShelves(GShelvesQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(gShelvesService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增测试题1")
    @ApiOperation("新增测试题1")
    @PreAuthorize("@el.check('gShelves:add')")
    public ResponseEntity<Object> createGShelves(@Validated @RequestBody GShelves resources){
        gShelvesService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改测试题1")
    @ApiOperation("修改测试题1")
    @PreAuthorize("@el.check('gShelves:edit')")
    public ResponseEntity<Object> updateGShelves(@Validated @RequestBody GShelves resources){
        gShelvesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除测试题1")
    @ApiOperation("删除测试题1")
    @PreAuthorize("@el.check('gShelves:del')")
    public ResponseEntity<Object> deleteGShelves(@RequestBody Integer[] ids) {
        gShelvesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping(value = "/up")
    @Log("上传背景图片")
    public ResponseEntity<Object> up(@RequestParam("avatar") MultipartFile file) throws Exception{
        // 文件大小验证
        FileUtil.checkSize(properties.getAvatarMaxSize(), file.getSize());
        // 验证文件上传的格式
        String image = "gif jpg png jpeg";
        String fileType = FileUtil.getExtensionName(file.getOriginalFilename());
        if(fileType != null && !image.contains(fileType)){
            throw new BadRequestException("文件格式错误！, 仅支持 " + image +" 格式");
        }
        File files = FileUtil.upload(file, properties.getPath().getAvatar());
        String s= Objects.requireNonNull(files).getPath();
        System.out.println("===============");
        System.out.println(s);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
